package com.example.consumer.controller;

import com.example.common.DubboPracticeConstants;
import com.example.service.CommonService;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.rpc.RpcContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author zhouyp
 * @program dubbo-practice
 * @description
 * @create 2020-07-08
 */
@RestController
public class CommonController {

	@Reference(version = "1.0.0")
	private CommonService commonService;

	@GetMapping("/host")
	public String currHost(@RequestParam(required = false) String host,
	                       HttpServletRequest request) {
		if (host == null) {
			host = request.getRemoteAddr();
		}
		RpcContext.getContext()
				.setAttachment(DubboPracticeConstants.TRANSPORT_IP_KEY, host);
		commonService.sayHello();
		return "当前host是：" + host;
	}

}
